package com.esgpro.esg_api.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.esgpro.esg_api.pojo.User;
import com.esgpro.esg_api.service.Impl.UserServiceImpl;
import com.esgpro.esg_api.util.DateUtil;
import com.esgpro.esg_api.util.JSONResult;
import com.esgpro.esg_api.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

@RestController
@Api(tags = "用户接口列表")
//@SaCheckLogin
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl service;
    @Autowired
    JSONResult result;
    @Autowired
    DateUtil dateUtil;


    @PostMapping("/register")
    public JSON register(
            @RequestParam @ApiParam("用户名") String username,
            @RequestParam String password,
            @RequestParam String contact,
            @RequestParam String type
    ) {
        //八位数自生成账号(前两位：联系方式前两位；中四位：建号月日；后两位：[个人：01,企业10])
        String s = contact.substring(0, 2) + dateUtil.getAccountDate() + service.getAccountByType(type);
        System.out.println(s);
        int account = Integer.parseInt(s) + new Random().nextInt(999);
        HashMap<String, Object> map = new HashMap<>();
        if (service.insertOne(new User(0, username, account, password, contact, type, 1))) {

            map.put("username", username);
            map.put("password", password);
            map.put("contact", contact);
        }
        List<User> list = (List<User>) service.selectByMap(1, 1, map).get("list");
        return result.putMapInResult("user", list.get(0));
    }


    @DeleteMapping("/deleteUser")
    public JSON deleteUserById(@RequestParam int id) {
        return result.getResult(service.deleteOneById(id));
    }


    @GetMapping("selectUser")
    public JSON selectUserById(int id) {
        return result.getDataResult(service.selectOneById(id));
    }

    @PutMapping("/updateUser")
    public JSON updateById(
            @RequestParam int id,
            @RequestParam String username,
            @RequestParam int account,
            @RequestParam String password,
            @RequestParam String contact,
            @RequestParam String type,
            @RequestParam int level) {
        return result.getResult(service.updateById(new User(id, username, account, password, contact, type, level)));
    }


    @GetMapping("/getAll")
    public JSON selectAll() {
        return result.getDataResult(service.selectAll());
    }

    @PostMapping("/selectMap")
    public JSON selectMessageByMap(@RequestParam int current, @RequestParam int size, @RequestParam(required = false) String keys, @RequestParam(required = false) String values) {
        HashMap<String, Object> map = new HashMap<>();
        List<String> key = StringUtil.StringFormat(keys);
        List<String> value = StringUtil.StringFormat(values);
        System.out.println("keys:" + key);
        System.out.println("values:" + value);
        if (key.size() > 0) {
            for (int i = 0; i < key.size(); i++)
                map.put(key.get(i), value.get(i));
        }

        return result.getDataResult(service.selectByMap(current, size, map));
    }

    @GetMapping("/loginOut")
    public JSON loginOut() {
        StpUtil.logout();
        return result.getResult(true);
    }

}
